<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <el-input v-model="search" placeholder="请输入部门名称" class="handle-input mr10" clearable></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
        <el-button type="primary" icon="el-icon-plus" @click="addNewDepart" style="width: 150px;float: right">添加新部门 </el-button>
      </div>
      <el-table :data="departData" border style="width: 100%" class="table">
        <el-table-column prop="departNum" label="部门编号" align="center"/>
        <el-table-column prop="departName" label="部门名称" align="center"/>
        <el-table-column prop="departNote" label="部门信息说明" align="center" width="300px"/>
        <el-table-column fixed="right" label="操作" width="180" align="center">
          <template #default="scope">
            <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination :total="total" :current-page="currentPage" :page-size="currentPageSize" :page-sizes="[3,6,10]"
                       layout="total, sizes, prev, pager, next, jumper" @size-change="pageSizeChange" @current-change="pageChange">
        </el-pagination>
      </div>
    </div>

    <!-- 添加弹出框 -->
    <el-dialog title="添加用户信息" v-model="addDepartVisible" width="50%">
      <el-form ref="addDepartform" :model="addDepartform" label-width="150px">
        <el-form-item label="部门名称：" prop="departName">
          <el-input v-model="addDepartform.departName"></el-input>
        </el-form-item>
        <el-form-item label="部门信息说明：" prop="departNote">
          <el-input type="textarea" v-model="addDepartform.departNote" :autosize="{minRows: 1, maxRows: 4}" :style="{width: '100%'}"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
                <span class="dialog-footer">
                    <el-button @click="addDepartVisible = false">取 消</el-button>
                    <el-button type="primary" @click="addDepartFunc">确 定</el-button>
                </span>
      </template>
    </el-dialog>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑部门信息" v-model="editVisible" width="50%">
      <el-form ref="form" :model="form" label-width="150px">
        <el-form-item label="部门编号：" prop="departNum">
          <el-input v-model="form.departNum" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="部门名称：" prop="departName">
          <el-input v-model="form.departName"></el-input>
        </el-form-item>
        <el-form-item label="部门信息说明：" prop="departNote">
          <el-input type="textarea" v-model="form.departNote" :autosize="{minRows: 1, maxRows: 4}" :style="{width: '100%'}"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
                <span class="dialog-footer">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveEdit">确 定</el-button>
                </span>
      </template>
    </el-dialog>

  </div>
</template>

<script>
import {getAllDeparts, editDepart, addDepart, delDepart} from "../../api/api";
export default {
  data() {
    return {
      addDepartVisible: false,
      search: '',
      currentPage: 1,
      currentPageSize: 6,
      total: null,
      tableData: [],
      departData: [],
      editVisible: false,
      form: {},
      addDepartform: {},
      id: -1,//记录删除的行
      idx: -1// 记录编辑的行
    };
  },
  created() {
    this.getData();
  },
  methods: {
    addNewDepart(){
      this.addDepartVisible = true
    },
    // 添加部门
    addDepartFunc() {
      let y = {
        sdDepartmentName: this.addDepartform.departName,
        sdDepartmentInfo: this.addDepartform.departNote,
      }
      console.log('添加的部门信息：',y)
      addDepart(y).then(res => {
        console.log('添加部门接口信息：', res.data)
        if (res.data.status == 200){
          this.addDepartVisible = false
          this.$message.success('添加部门成功！')
          this.getData()
          this.addDepartform = {}
        }else{
          this.$message.error('添加部门失败!')
        }
      })
    },
    // 模糊搜索
    searchList() {
      if (this.tableData.length > 0){
        let pageData = this.tableData.filter((item) =>
            (item.departNum!==null && item.departNum.toString().includes(this.search))||
            item.departName.includes(this.search)||
            (item.departNote!==null && item.departNote.includes(this.search))
        )
        this.departData = pageData.filter((item, index) =>
            index < this.currentPage * this.currentPageSize && index >= this.currentPageSize * (this.currentPage - 1)
        )
        this.total = pageData.length
      }
    },
    // 获取所有部门信息
    getData() {
      getAllDeparts().then((res) =>{
        console.log('res.data', res.data)
        if (res.data.code == 403){
          this.$router.push('/')
        }else{
          this.tableData = []
          res.data.data.forEach((item) => {
            let add = {
              departNum: item.sdDepartmentId,
              departName: item.sdDepartmentName,
              departNote: item.sdDepartmentInfo
            }
            this.tableData.push(add)
          })
          this.searchList()
        }
      })
    },
    // 编辑按钮点击确定
    saveEdit(){
      let y = {
        sdDepartmentId:this.form.departNum,
        sdDepartmentName: this.form.departName,
        sdDepartmentInfo: this.form.departNote,
      }
      editDepart(y).then(res=>{
        console.log('编辑用部门接口信息：', res.data)
        if (res.data.status == 200){
          this.editVisible = false
          this.form = {}
          this.$message.success(`成功修改`+y.sdDepartmentName+`部门信息!`);
          this.getData()
        }else{
          this.$message.error(`修改`+y.sdDepartmentName+`部门信息失败!`);
        }
      })
    },
    // 触发搜索按钮
    handleSearch() {
      this.currentPage = 1
      this.searchList();
    },
    // 编辑操作
    handleEdit(row) {
      this.form = row;
      this.editVisible = true;
    },
    // 删除部门
    handleDelete(row){
      const delNum = row.departNum
      // 二次确认删除
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning"
      }).then(() => {
        delDepart(delNum).then((res) =>{
          console.log('信息：', res.data)
          if (res.data.code == 401){
            this.$router.push('/')
          }
          if(res.data.status == 200){
            this.$message.success(`成功删除`+row.departName+`!`);
            this.getData()
          }else{
            this.$message.success(`删除`+row.departName+`失败!`);
          }
        })
      }).catch(() => {

      });
    },
    //当每页数量改变
    pageSizeChange(size) {
      this.currentPageSize = size
      this.searchList()
    },
    // 当当前页改变
    pageChange(page) {
      this.currentPage = page
      this.searchList()
    }
  }
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
